﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net;
using System.IO;

namespace MovieInfoView
{
    /// <summary>
    /// Interaction logic for MovieCover.xaml
    /// </summary>
	/// <remarks>
	/// Represents a control that displays a movie cover.
	/// </remarks>
    public partial class MovieCover : Image
    {
        public bool selected = false;
        private int scaleVal = 20;

        private System.Drawing.Bitmap bmp;

        private int baseWidth = 80,
                    baseHeight = 120,
                    largeWidth = 100,
                    largeHeight = 140;

        private BitmapImage bi = null;

        public int dlProgress = 0;
        public bool dlComplete = false;

        public event EventHandler DownloadComplete;
        public event EventHandler DownloadProgressChanged;


        public MovieCover()
        {
            InitializeComponent();
            this.Cursor = Cursors.Hand;
            this.Opacity = 0.5;
        }

        public void downloadImage(string url)
        {
            bi = new BitmapImage(new Uri(url));
            this.Source = bi;
            this.InvalidateVisual();

            bi.DownloadCompleted += new EventHandler(bi_DownloadCompleted);
            bi.DownloadProgress += new EventHandler<DownloadProgressEventArgs>(bi_DownloadProgress);

            
        }

        void bi_DownloadProgress(object sender, DownloadProgressEventArgs e)
        {
            this.dlProgress = e.Progress;
            this.DownloadProgressChanged(this, new EventArgs());
        }

        void bi_DownloadCompleted(object sender, EventArgs e)
        {
            BitmapEncoder enc = null;
            string url = bi.UriSource.AbsoluteUri;

            switch (url.Substring(url.Length - 4))
            {
                case ".bmp":
                    enc = new BmpBitmapEncoder();
                    break;
                case ".gif":
                    enc = new GifBitmapEncoder();
                    break;
                case ".jpg":
                    enc = new JpegBitmapEncoder();
                    break;
                case ".png":
                    enc = new PngBitmapEncoder();
                    break;
                case ".tif":
                    enc = new TiffBitmapEncoder();
                    break;
            }

            MemoryStream ms = new MemoryStream();

            enc.Frames.Add(BitmapFrame.Create(bi));
            enc.Save(ms);

            System.Drawing.Bitmap obConv = new System.Drawing.Bitmap(ms);
            bmp = new System.Drawing.Bitmap(obConv);

            this.dlComplete = true;
            this.DownloadComplete(this, new EventArgs());
        }


        public System.Drawing.Bitmap getBitmapImage()
        {
            return bmp;
        }

        protected override void OnMouseEnter(MouseEventArgs e)
        {
            base.OnMouseEnter(e);

            if (!selected)
            {
                this.Width = largeWidth;
                this.Height = largeHeight;
                this.Opacity = 1;
            }
        }

        protected override void OnMouseLeave(MouseEventArgs e)
        {
            base.OnMouseLeave(e);

            if (!selected)
            {
                this.Width = baseWidth;
                this.Height = baseHeight;
                this.Opacity = 0.5;
            }
        }

        public void deSelect()
        {
            this.Width = baseWidth;
            this.Height = baseHeight;


            this.selected = false;
            this.Opacity = 0.5;
        }

        public void setSelected()
        {
            this.Width = largeWidth;
            this.Height = largeHeight;


            this.selected = true;
            this.Opacity = 1;
        }

        protected override void OnMouseDown(MouseButtonEventArgs e)
        {
            base.OnMouseDown(e);
        }
    }
}
